﻿namespace TrickleDemo.View
{
    using System;
    using System.Collections.Generic;
    using Microsoft.Phone.Controls;
    using Model;
    using WP7Contrib.Collections;

    public partial class MainPage : PhoneApplicationPage
    {
        private readonly ObservableCollection<Product> people = new ObservableCollection<Product>();
        private TrickleAllToCollection<Product> trickler;
        public MainPage()
        {
            InitializeComponent();

            trickler = new TrickleAllToCollection<Product>(TrickeAdded, TrickeCompleted);
        }

        private void LayoutRoot_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            peopleList.ItemsSource = people;
        }

        private void trickleAdd_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var count = Convert.ToInt32(itemCount.Text);
            var addPeople = new List<Product>();
            for (var i = 0; i < count; i++)
            {
                var p = new Product()
                            {
                                Name = RandomTextGenerator.CapitalisedText(3, 7),
                                Description = RandomTextGenerator.Description(10, 30),
                                Image = "../Resources/Images/Product.png"
                            };

                addPeople.Add(p);
            }

            trickler.Start(142, addPeople, people);
        }

        private void TrickeAdded()
        {
            peopleCount.Text = "People Count = " + people.Count;

            if (gcCollectOnAdd.IsChecked.GetValueOrDefault())
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }

        private void TrickeCompleted()
        {
        }

        private void directAdd_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var count = Convert.ToInt32(itemCount.Text);
            var addPeople = new List<Product>();
            for (var i = 0; i < count; i++)
            {
                var p = new Product()
                {
                    Name = RandomTextGenerator.CapitalisedText(3, 7),
                    Description = RandomTextGenerator.Description(10, 30),
                    Image = "../Resources/Images/Product.png"
                };

                addPeople.Add(p);
            }

            addPeople.ForEach(ap =>
                                  {
                                      people.Add(ap);
                                      peopleCount.Text = "People Count = " + people.Count;

                                      if (gcCollectOnAdd.IsChecked.GetValueOrDefault())
                                      {
                                          GC.Collect();
                                          GC.WaitForPendingFinalizers();
                                      }
                                  });

          
        }

        private void clearList_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            people.Clear();
            peopleCount.Text = string.Empty;
        }
    }

    public static class RandomNumberGenerator
    {
        private static readonly Random Random = new Random((int)DateTime.Now.Ticks + 42);

        public static int Number(int min, int max)
        {
            return Random.Next(min, max);
        }
    }

    public static class RandomTextGenerator
    {
        private static readonly Random Random = new Random((int)DateTime.Now.Ticks - 42);

        public static string Text(int minLength, int maxLength)
        {
            var length = RandomNumberGenerator.Number(minLength, maxLength);
            var text = new char[length];

            for (var i = 0; i < length; i++)
            {
                text[i] = (char)Random.Next(97, 122);
            }

            return new string(text);
        }

        public static string CapitalisedText(int minLength, int maxLength)
        {
            var length = RandomNumberGenerator.Number(minLength, maxLength);
            var text = new char[length];

            for (var i = 0; i < length; i++)
            {
                if (i == 0)
                    text[i] = (char)Random.Next(65, 90);
                else text[i] = (char) Random.Next(97, 122);
            }

            return new string(text);
        }

        public static string Name(int minLength, int maxLength)
        {
            var count = RandomNumberGenerator.Number(1, 3);

            var name = string.Empty;
            for(var i = 0; i < count; i++)
            {
                if (i != 0)
                    name += "-";

                name += CapitalisedText(minLength, maxLength);
            }

            return name;
        }

        public static string Description(int minLength, int maxLength)
        {
            var count = RandomNumberGenerator.Number(minLength, maxLength);

            var description = string.Empty;
            for (var i = 0; i < count; i++)
            {
                if (i == 0)
                    description += CapitalisedText(4, 8);
                else
                {
                    description += " " + Text(4, 8);
                }
            }

            return description;
        }
    }
}